package com.boss.ccs.rnb;

import com.boss.Messages;
import com.boss.core.BossConstants;
import com.boss.core.form.BossForm;
import com.boss.core.form.BossFormHelper;
import com.boss.core.util.BossUtil;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.StaplerRequest;

import java.util.Map;

/**
 * Boss RNB Form
 * @author Momin Noor Khan
 */
public class BossRNBForm extends BossForm {
    private boolean isTargetSection;
    private String targetProfile;
    private boolean isExistingBuildBlock;
    private String existingBuildNumber;

    //Getters and Setters
    public boolean isTargetSection() {
        return isTargetSection;
    }

    public void setTargetSection(boolean targetSection) {
        isTargetSection = targetSection;
    }

    public String getTargetProfile() {
        return targetProfile;
    }

    public void setTargetProfile(String targetProfile) {
        this.targetProfile = targetProfile;
    }

    public boolean isExistingBuildBlock() {
        return isExistingBuildBlock;
    }

    public void setExistingBuildBlock(boolean existingBuildBlock) {
        isExistingBuildBlock = existingBuildBlock;
    }

    public String getExistingBuildNumber() {
        return existingBuildNumber;
    }

    public void setExistingBuildNumber(String existingBuildNumber) {
        this.existingBuildNumber = existingBuildNumber;
    }

    public BossRNBForm(StaplerRequest req, JSONObject jsonForm, String project) {
        super(req, jsonForm, project);
        isTargetSection = BossUtil.contains(getJsonTaskSection(), BossConstants.KEY_TARGET);
        targetProfile = BossFormHelper.getTargetProfile(isTargetSection, req);
        isExistingBuildBlock = BossUtil.contains(getJsonTaskSection(), BossConstants.KEY_EXISTING_BUILD_BLOCK);
        existingBuildNumber = BossUtil.getStringValueFromJson(getJsonTaskSection(), BossConstants.KEY_EXISTING_BUILD_BLOCK,
                BossConstants.KEY_EXISTING_BUILD_NUMBER);
    }

    @Override
    public void prepareUrlParamMap() {
        super.prepareUrlParamMap();
        Map<String, String> map = getUrlMap();
        map.put(BossConstants.URL_PARAM_TARGET_PROFILE, targetProfile);
        if(isExistingBuildBlock) {
            map.put(BossConstants.URL_PARAM_EXISTING_BUILD_NUMBER, existingBuildNumber);
        }
        setUrlMap(map);
    }

    @Override
    public boolean isValid() {
        boolean valid = true;
        if (!checkMandatoryFields() |
                !checkExistingBuildNumber() |
                !checkTargetProfile()
                ) {
            valid = false;
        } else if (!BossFormHelper.checkAvailableJob(getRequest(), getAvailableJob())) {
            valid = false;
        }
        return valid;
    }

    //Validation methods
    public boolean checkMandatoryFields() {
        boolean valid = true;
        StaplerRequest req = getRequest();
        String release = getRelease();
        String task = getTask();
        if (!BossFormHelper.checkRelease(req, release) | !BossFormHelper.checkTask(req, task)) {
            valid = false;
        }
        return valid;
    }

    public boolean checkExistingBuildNumber() {
        boolean valid = true;
        StaplerRequest req = getRequest();
        boolean existingBuildBlock = isExistingBuildBlock();
        String existingBuildNumber = getExistingBuildNumber();
        if (existingBuildBlock && BossUtil.isBlankOrDummy(existingBuildNumber)) {
            BossFormHelper.setValidationErrorMsg(req, Messages.Boss_MandatoryExistingBuildNumberIfChecked());
            valid = false;
        }
        return valid;
    }

    public boolean checkTargetProfile() {
        boolean valid = true;
        StaplerRequest req = getRequest();
        String task = getTask();
        String targetProfile = getTargetProfile();
        if (isTargetSection()) {
            if (StringUtils.isBlank(targetProfile)) {
                BossFormHelper.setValidationErrorMsg(req, Messages.Boss_MandatoryTarget(task));
                valid = false;
            }
        }
        return valid;
    }
}
